<?php
class Category_model extends MY_Model
{
    private $table = 'ym_item_category';
    public function __construct()
    {
        parent::__construct();
    }
    public function getCatList()
    {
        $sql = "SELECT * FROM ym_item_category";
        $query = $this->db->query($sql);

        return $query->result_array();
    }
    public function get_cat_by_id($id)
    {
        $this->db->where("cat_id",$id);
        $query=$this->db->get($this->table);
        return $query->row_array();
    }
   
    public function get_cat_by_parent_id($parent_id)
    {
        $this->db->where("parent_id",$parent_id);
        $query=$this->db->get($this->table);
        return $query->result_array();
    }
    public function get_cat_list($notlevel_3)
    {
        $where = "";
        if($notlevel_3){
            $where  = " where p_level <> 3";
        }
        /*
        //$this->db->where("parent_id",$parent_id);
        $this->db->order_by("cat_id","asc");
        $query=$this->db->get($this->table);
        return $query->result_array();
        */
        $sql = "SELECT * FROM ym_item_category {$where} ORDER BY sort_order ASC,cat_id ASC";
        $query = $this->db->query($sql);
        return $query->result_array();        
    }

  
    public function add_cat($data)
    {
        $res= $this->db->insert($this->table,$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }


  
    public function edit_cat($id,$data)
    {
        $this->db->where('cat_id',$id);
        return $this->db->update($this->table,$data);
    }        
    //删除分类
    public function del_cat($del_ids)
    {
        $sql = "DELETE FROM ym_item_category WHERE cat_id ={$del_ids}";
        $query = $this->db->query($sql);
        return true;
    }
    
    
    /**
     * 获取获取当前分类及所有父分类名字
     *@author KevinBai
     */
    public function get_level_catname_bycid($parent_id){
       $sql = "SELECT * FROM ym_item_category WHERE cat_id='{$parent_id}'";
       $query = $this->db->query($sql);
       $tem = $query->row_array();
       $pre_name = "";
       if($query->num_rows()){
           $pre_name.= $this->get_level_catname_bycid($tem['parent_id']); 
           $pre_name.= $tem['cat_name'].'->';   
        }
        return $pre_name;
    }       
     
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */